package com.aidong.login.action;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.json.annotations.JSON;
import org.springframework.beans.factory.annotation.Autowired;

import com.aidong.jobs.async.SmsTask;
import com.aidong.jobs.constants.JobConst;
import com.aidong.login.biz.MemberInformationBiz;
import com.aidong.login.biz.MemberReigsterBiz;
import com.aidong.login.entity.Member;
import com.aidong.util.RandomUtil;
import com.aidong.util.bus.SecurityUtils;
import com.opensymphony.xwork2.ModelDriven;

@ParentPackage("json")
@Namespace("/")
public class MemberInformationAction implements ModelDriven<Member> {

	private Member member;

	@Override
	public Member getModel() {
		if (member == null) {
			member = new Member();
		}
		return member;
	}

	@Autowired
	private MemberInformationBiz memberInformationBiz; // 会员信息service
	@Autowired
	private MemberReigsterBiz memberBiz; // 验证是否注册
	@Autowired
	SmsTask smsTask;

	private Map<String, Object> map;

	@JSON
	public Map<String, Object> getMap() {
		return map;
	}

	/**
	 * 获得手机验证码 忘记密码
	 * 
	 * @param phoneNo
	 * @return
	 */
	@Action(value = "getremembercode", results = { @Result(name = "succ", type = "json", params = { "root", "map" }) })
	public String getRememberCode() {
		map = new HashMap<String, Object>();
		try {
			String phoneNo = member.getMobilePhone(); // 手机号码
			String codes=member.getCode();
			if(StringUtils.isBlank(codes)){
				map.put("code", 0);
				map.put("msg", "请下载最新版本");
				return "succ";
			}
			String[] codeMap=JobConst.codeMap.get(phoneNo);
			if(codeMap==null){
				map.put("code", 0);
				map.put("msg", "验证码已过期");
				return "succ";
			}
			if (!"".equals(phoneNo) && phoneNo != null) {
				if(!codes.equalsIgnoreCase(codeMap[0])){
					map.put("code", 0);
					map.put("msg", "验证码输入有误");
					return "succ";
				}
				JobConst.codeMap.remove(phoneNo);
				int count = memberBiz.getRegistered(phoneNo);
				if (count > 0) {
					String captcha = RandomUtil.randomNumber(6);
					int code=Integer.valueOf(captcha);
					smsTask.captcha(phoneNo,String.valueOf(code));
					map.put("code", 1);
					map.put("result", code);
					return "succ";
				} else {
					map.put("code", 0);
					map.put("msg", "该账号不存在");
					return "succ";
				}
			} else {
				map.put("code", 0);
				map.put("msg", "参数缺失");
				return "succ";
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", 0);
			map.put("msg", "系统异常");
			return "succ";
		}
	}

	/**
	 * 修改会员密码
	 * 
	 * @param phoneNo
	 * @return
	 */
	@Action(value = "editmemberpsw", results = { @Result(name = "succ", type = "json", params = { "root", "map" }) })
	public String editMemberPsw() {
		map = new HashMap<String, Object>();
		try {
			String phoneNo = member.getMobilePhone(); // 手机号码

			member.setPassword(SecurityUtils.encryptDbMemberPassword(member.getPassword()));
			String password = member.getPassword(); // 密码
			if ((!"".equals(phoneNo) && phoneNo != null) && (!"".equals(password) && password != null)) {
				Map<String, Object> paraMap = new HashMap<String, Object>();
				paraMap.put("password", password);
				paraMap.put("phoneNo", phoneNo);
				memberInformationBiz.editMemberPsw(paraMap);
				map.put("code", 1);
				map.put("result", "修改成功");
				return "succ";
			} else {
				map.put("code", 0);
				map.put("msg", "参数缺失");
				return "succ";
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", 0);
			map.put("msg", "系统异常");
			return "succ";
		}
	}

	/**
	 * 修改会员手机号码
	 * 
	 * @param phoneNo
	 * @return
	 */
	@Action(value = "editmemberphone", results = { @Result(name = "succ", type = "json", params = { "root", "map" }) })
	public String editMemberPhone() {
		map = new HashMap<String, Object>();
		try {
			String phoneNo = member.getMobilePhone(); // 手机号码
			String idongId = member.getIdongId(); // 爱动号
			if ((!"".equals(phoneNo) && phoneNo != null) && (!"".equals(idongId) && idongId != null)) {
				int count = memberBiz.getRegistered(phoneNo);
				if (count > 0) {
					map.put("code", 0);
					map.put("msg", "该手机号码已经被绑定其它的爱动号");
					return "succ";
				} else {
					Map<String, Object> paraMap = new HashMap<String, Object>();
					paraMap.put("idongId", idongId);
					paraMap.put("phoneNo", phoneNo.trim());
					memberInformationBiz.editMemberPhone(paraMap);
					map.put("code", 1);
					map.put("result", "修改成功");
					return "succ";
				}
			} else {
				map.put("code", 0);
				map.put("msg", "参数缺失");
				return "succ";
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", 0);
			map.put("msg", "系统异常");
			return "succ";
		}
	}
}
